package com.javacoo.xservice.example.service.impl;

import java.util.Optional;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javacoo.event.client.annotation.EventHandler;
import com.javacoo.event.client.starter.EventHolder;
import com.javacoo.events.utils.BizEventPublisher;
import com.javacoo.xservice.base.support.AbstractAssert;
import com.javacoo.xservice.base.utils.CacheUtil;
import com.javacoo.xservice.example.bean.dto.ExampleDto;
import com.javacoo.xservice.example.bean.dto.ExampleDto2;
import com.javacoo.xservice.example.constant.ErrorCodeConstants;
import com.javacoo.xservice.example.dao.ExampleDao;
import com.javacoo.xservice.example.service.ExampleService;

import lombok.extern.slf4j.Slf4j;

/**
 * 案例数据服务接口实现
 * <p>说明:</p>
 * <li></li>
 * @author DuanYong
 * @since 2017年7月14日下午1:30:18
 */
@Service
@Slf4j
public class ExampleServiceImpl implements ExampleService {
    /**
     * 查询缓存KEY:id
     */
    private static final String QUERY_CACHE_KEY = "query:%1$s";
    /**
     * 查询缓存超时时间:1 分钟
     */
    private static final int QUERY_CACHE_TIMEOUT = 60 * 1;
    /**
     * 缓存工具类
     */
    @Autowired
    protected CacheUtil cacheUtil;
	@Autowired
    private ExampleDao exampleDao;


    /**
     * 获取示例信息函数
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2021/7/14 16:39
     * @param id: id
     * @return: ExampleDto 示例信息
     */
    private Function<String, ExampleDto> getExampleInfoFunction = (String id)-> exampleDao.getExampleInfo(id);

	@Override
	public Optional<ExampleDto> getExampleInfo(String id) {
		AbstractAssert.isNotBlank(id, ErrorCodeConstants.SERVICE_GET_EXAMPLE_INFO_ID);
        // 从缓存中获取数据
        String cacheKey = String.format(QUERY_CACHE_KEY,id);
        ExampleDto exampleDto = new ExampleDto();
        exampleDto.setData("这是测试数据"+id);
        exampleDto.setId("1"+id);
        ExampleDto exampleDto3 = new ExampleDto();
        exampleDto3.setData("测试案例3"+id);
        exampleDto3.setId("3"+id);
        ExampleDto exampleDto4 = new ExampleDto();
        exampleDto4.setData("测试案例4"+id);
        exampleDto4.setId("4"+id);
        EventHolder.getEventHelper().get().postDelay(exampleDto,1000);
        try {
            BizEventPublisher.publish(exampleDto);
            BizEventPublisher.publish(exampleDto3);
            BizEventPublisher.publish(exampleDto4);
            BizEventPublisher.publish(exampleDto,"主题");
            BizEventPublisher.publish(exampleDto,"","1.0");
            BizEventPublisher.publish(exampleDto,"主题","1.0");
            ExampleDto2 exampleDto2 = new ExampleDto2();
            exampleDto2.setData("这是测试数据2"+id);
            exampleDto2.setId("2"+id);
            BizEventPublisher.publish(exampleDto2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Optional.of(exampleDto);
        //return Optional.ofNullable(cacheUtil.getCacheValueFunction(cacheKey,QUERY_CACHE_TIMEOUT,ExampleDto.class, getExampleInfoFunction,id));
	}
    //@MySubscribe
	public void test(ExampleDto exampleDto){
        log.info("事件处理1:{}",exampleDto);
    }
    //@MySubscribe
    public void test2(ExampleDto exampleDto){
        log.info("事件处理2:{}",exampleDto);
    }
    @EventHandler
    public void test3(ExampleDto exampleDto){
        log.info("事件处理3:{}",exampleDto);
    }
    @EventHandler
    public void test4(ExampleDto exampleDto){
        log.info("事件处理4:{}",exampleDto);
    }
}
